﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PluginCommon;

namespace KtzEditer
{
    public partial class MainUserControl : PluginFormBase
    {
        private byte[] file;
        private string fileName;
        private const int DllBase = 0x10000000;
        private const int PSectionBase = 0x10000000;

        public MainUserControl()
        {
            InitializeComponent();
        }

        private void ReplaceButton_Click(object sender, EventArgs e)
        {
            byte[] toSearchByte = StringToHexBytes(skill0TextBox.Text);
            byte[] toReplaceByte = StringToHexBytes(skill1TextBox.Text);
            int start = FindBytes(file, toSearchByte, 0);
            int end = -1;
            if (start < 0)
            {
                MessageBox.Show(this, "没有找到");
            }
            else
            {
                //ReplaceMethodV1(start, end, toReplaceByte);

                byte[] pushCode = GetPushCode((uint)(start + DllBase));
                int pushPostion = FindBytes(file, pushCode, 0);
                if (pushPostion < 0)
                {
                    MessageBox.Show(this, "找不到代码位置");
                    return;
                }

                int newSkilPosition = 0;
                for (int i = file.Length - 1; i > 0; i--)
                {
                    if (file[i] != 0)
                    {
                        newSkilPosition = i;
                        break;
                    }
                }

                newSkilPosition += (4 - newSkilPosition % 4) + 4;

                for (int i = newSkilPosition, j = 0; j < toReplaceByte.Length; j++, i++)
                {
                    file[i] = toReplaceByte[j];
                }

                for (int i = start, j = 0; j < toSearchByte.Length; j++, i++)
                {
                    file[i] = 0;
                }

                byte[] newPushCode = GetPushCode((uint)(newSkilPosition + DllBase));
                for (int i = pushPostion, j = 0; j < newPushCode.Length; i++, j++)
                {
                    file[i] = newPushCode[j];
                }

                MessageBox.Show(this, "替换成功，享受去吧");
            }
        }

        private void ReplaceMethodV1(int start, int end, byte[] toReplaceByte)
        {
            for (int i = start; i < file.Length; i++)
            {
                if (file[i] == 0)
                {
                    end = i;
                    break;
                }
            }
            for (int i = end + 1; i < file.Length; i++)
            {
                if (file[i] == 0)
                {
                    end++;
                }
                else
                {
                    break;
                }
            }

            int length = end - start + 1;
            if (length < toReplaceByte.Length + 2)
            {
                MessageBox.Show(this, "被替换技能长度太短");
                return;
            }

            fileHex.Text = ByteToHexString(file, start, end);

            for (int i = start; i <= end; i++)
            {
                if (i - start > toReplaceByte.Length - 1)
                {
                    file[i] = 0;
                }
                else
                {
                    file[i] = toReplaceByte[i - start];
                }
            }

            MessageBox.Show(this, "替换成功");
        }

        private void OpenButton_Click(object sender, EventArgs e)
        {
            var openFileDialog = new OpenFileDialog();
            var dialogResult = openFileDialog.ShowDialog(this);
            if (dialogResult == System.Windows.Forms.DialogResult.OK)
            {
                fileName = openFileDialog.FileName;
                file = File.ReadAllBytes(fileName);
            }
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            File.Delete(fileName + ".bak");
            File.Copy(fileName, fileName + ".bak");
            File.WriteAllBytes(fileName, file);
        }

        private void Skill0TextBox_TextChanged(object sender, EventArgs e)
        {
            skill0Hex.Text = StringToHexString(skill0TextBox.Text);
        }

        private void Skill1TextBox_TextChanged(object sender, EventArgs e)
        {
            skill1Hex.Text = StringToHexString(skill1TextBox.Text);
        }

        private static string StringToHexString(string value)
        {
            byte[] transcoded = Encoding.Default.GetBytes(value);
            var stringBuilder = new StringBuilder();
            for (int i = 0; i < transcoded.Length; i++)
            {
                stringBuilder.AppendFormat("{0} ", transcoded[i].ToString("x"));
            }
            return stringBuilder.ToString();
        }

        private static string ByteToHexString(byte[] value, int start, int end)
        {
            var stringBuilder = new StringBuilder();
            for (int i = start; i <= end; i++)
            {
                stringBuilder.AppendFormat("{0} ", value[i].ToString("x"));
            }
            return stringBuilder.ToString();
        }

        private static byte[] StringToHexBytes(string value)
        {
            byte[] transcoded = Encoding.Default.GetBytes(value);
            return transcoded;
        }

        public static int FindBytes(byte[] a, byte[] b, int c)
        {
            int p = -1;
            for (var i = 0 + c; i < a.Length; i++)
            {
                if (a[i].Equals(b[0]))
                {
                    p = i;
                    for (var j = 0; j < b.Length; j++)
                    {

                        if (!a[i + j].Equals(b[j]))
                        {
                            p = -1;
                            break;
                        }
                    }
                    if (p != -1) break;
                }
            }

            return p;
        }

        public static byte[] GetPushCode(uint value)
        {
            var code = new byte[5];
            code[0] = 0x68;
            code[1] = (byte)(value & 0x000000FF);
            code[2] = (byte)(value >> 8 & 0x000000FF);
            code[3] = (byte)(value >> 16 & 0x000000FF);
            code[4] = (byte)(value >> 24 & 0x000000FF);
            return code;
        }
    }
}
